package cn.newgrand.common;

import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageUtil {
	
	public static void gray(String source, String result){
		gray(new File(source), result);
	}
	
	public static void gray(File source, String result){
		try{
			BufferedImage src = ImageIO.read(source);
			ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
			ColorConvertOp op = new ColorConvertOp(cs, null);
			src = op.filter(src, null);
			ImageIO.write(src, "JPEG", new File(result));
		}
		catch (IOException e){
			e.printStackTrace();
		}
	}
	public static ImageIcon toGray(File source, String result){
		try{
			BufferedImage src = ImageIO.read(source);
			ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
			ColorConvertOp op = new ColorConvertOp(cs, null);
			src = op.filter(src, null);
			return new ImageIcon(src);
		}
		catch (IOException e){
			e.printStackTrace();
		}
		return null;
	}
}
